package general 
{
	import org.airmvc.Controller;
	import org.airmvc.ModulesManager;
	import utils.T;
	import vo.GeneralVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class GeneralC extends Controller 
	{
		
		public function GeneralC() 
		{
			super();
		}
		
		override protected function addListeners():void 
		{
			interest(Nws.GENERAL_OPEN, handleOpen);
			interest(Nws.GENERAL_SHOW_HEAD, handleShowHead);
			
			receive(CLOSE, handleClose);
			receive(SWITCH_CASTLE_INFO, switchCastleInfo);
		}
		
		private function switchCastleInfo():void 
		{
			flag = flag == 1?2:1;
			broadcast(Nws.INFOMATION_SWITCH_CITY, flag);
		}
		
		private function handleClose(mode:int):void 
		{
			if (mode == Cnt.MODE_STRATEGY)
			{
				broadcast(Nws.INFOMATION_UPDATE_ORDER, 2);
				broadcast(Nws.MENU_SHOW, 4);
			}
			else if (mode == Cnt.MODE_RECONNOITER)
			{
				broadcast(Nws.MENU_SHOW, 15);
			}
			else if (mode == Cnt.MODE_FIGHT)
			{
				
			}
		}
		
		private function handleShowHead(gid1:int,gid2:int = 0):void 
		{
			var g1:GeneralVO = T.db.getRecord(Cnt.VO_GENERAL, gid1);
			var g2:GeneralVO = null;
			if (gid2 != 0)
			{
				g2 = T.db.getRecord(Cnt.VO_GENERAL, gid2);
			}
			send(SHOW_HEAD, g1, g2);
		}
		
		private function handleOpen(gs:Array,mode:int):void 
		{
			if (mode == Cnt.MODE_RECONNOITER)
			{
				flag = 1;
				broadcast(Nws.INFOMATION_SWITCH_CITY, flag);
			}
			
			if (gs.length == 0 && mode == Cnt.MODE_RECONNOITER)
			{
				Msg.instance.show("该座城池没有武将", 160, 340);
			}
			else
			{
				Msg.instance.show("");
				broadcast(Nws.INFOMATION_UPDATE_ORDER, 3);
			}
			
			broadcast(Nws.MENU_SHOW, 0);
			send(SHOW_INFO, gs, mode);
		}
		
		override protected function startup(... args):void 
		{
			if (view == null)
			{
				//初始化视图
				view = new GeneralV();
				registerView(view);
			}
			else
			{
				send(SHOW_OR_HIDE,args[0]);
			}
		}
		
		private var flag:int;
		
		/**打开或关闭视图*/
		static public const SHOW_OR_HIDE:String = "show_or_hide";
		/**显示武将信息*/
		static public const SHOW_INFO:String = "show_info";
		/**显示将军头像*/
		static public const SHOW_HEAD:String = "show_head";
		/**关闭*/
		static public const CLOSE:String = "close";
		/**切换城池信息*/
		static public const SWITCH_CASTLE_INFO:String = "switch_castle_info";
	}
	
}